package DB;


import java.util.ArrayList;

import org.lightcouch.CouchDbClient;

import Utility.Base64;

import com.google.gson.JsonObject;

public class CouchDB {
	private CouchDbClient dbClient;
	public static final String REGDB = "regdb";
	public static final String REMDB = "remdb";
	public static final String RETDB = "retdb";
	
	
	
	public CouchDB(){
		dbClient =  new CouchDbClient("users", true, "http", "127.0.0.1", 5984, null, null);
	}
	
	public CouchDB(String dbType){
		dbClient =  new CouchDbClient(dbType, true, "http", "127.0.0.1", 5984, null, null);
	}
	
	/**
	 * Check the user exists by id
	 * @param email
	 */
	public boolean checkUserExist(String email){
		return dbClient.contains(email);
	}
	
	/**
	 * get the user by id
	 * @param email
	 * @return
	 */
	public JsonObject getUser(String email){
		JsonObject json = dbClient.find(JsonObject.class, email);
		return json;	
	}
	
	public boolean checkUserPasswd(String email, String pswdIn){
		
		JsonObject json = dbClient.find(JsonObject.class, email);
		return ("\""+Base64.EncoderBase64(pswdIn)+"\"").equals(String.valueOf(json.get("passwd")));
		//return MD5Tools.EncoderByMd5(pswdIn).equals(String.valueOf(json.get("password")));		
	}
	
	/**
	 * save a new user
	 * @return
	 */
	public void createUser(String email, String passwd){
		JsonObject  object = new JsonObject ();
		object.addProperty("_id", email);
		String passwdInDB = Base64.EncoderBase64(passwd);
		object.addProperty("passwd", passwdInDB);
		//save the message
		dbClient.save(object);
	}
	public void deleteUser(String userID){

		JsonObject jsonObject = dbClient.find(JsonObject.class, userID);
		dbClient.remove(jsonObject);
		// delete all its owned groups
		GroupTableDB groupTableDB = new GroupTableDB();
		groupTableDB.deleteGroupByOwner(userID);
		// delete all its related groups
		GroupAndUsersTableDB groupAndUsersTableDB = new GroupAndUsersTableDB();
		groupAndUsersTableDB.deleteGroupAndUsersByUser(userID);
		// delete all its related chats
		ChatTableDB chatTableDB = new ChatTableDB();
		chatTableDB.deleteChatByUser(userID);
	}
	
	public JsonObject selectUser(String userID){
		JsonObject jsonObject = dbClient.find(JsonObject.class, userID);
		return jsonObject;
	}
	
	public void updatePasswd(String email, String passwd){
		this.deleteUser(email);
		this.createUser(email, passwd);		
	}
	
	public ArrayList<JsonObject> selectUsersByGroup(String groupID){
		GroupAndUsersTableDB groupAndUsersTableDB = new GroupAndUsersTableDB();
		ArrayList<JsonObject> allDocs = groupAndUsersTableDB.selectGroupAndUsersByGroup(groupID);
		ArrayList<JsonObject> result = new ArrayList<JsonObject>();
		for(JsonObject json : allDocs){
			JsonObject user = selectUser(json.get(GroupAndUsersTableDB.USERFK).getAsString());
				result.add(user);
			}
		return result;
	}
	
	public static String eraseQuotation(String valueInDB){
		int len = valueInDB.length();
		return valueInDB.substring(1,len-1);
		
	}
	/**
	 * create tmp email in tmp email db: "reminder or register "
	 * @param email
	 * @param currentTime
	 */
	public void createTmpEmail(String email, long currentTime, String token){
		JsonObject  object = new JsonObject ();
		object.addProperty("_id", email);
		object.addProperty("time", currentTime);
		object.addProperty("token", token);
		//save the message
		dbClient.save(object);
	}
	/**
	 * delete tmp email in tmp email db: "reminder or register "
	 * @param emial
	 */
	public void deleteTmpEmail(String email){
		JsonObject json = dbClient.find(JsonObject.class, email);
		dbClient.remove(json);
	}
	/**
	 * update tmp email in tmp email db: "reminder or register "
	 * @param email
	 * @param currentTime
	 */
	public void updateTmpEmail(String email, long currentTime, String token){
		this.deleteTmpEmail(email);
		this.createTmpEmail(email, currentTime, token);		
	}
	
	/**
	 * get the tmp object from tmp db: reminder or register
	 * @param email
	 * @return
	 */
	public JsonObject getTmpObject(String email){
		JsonObject json = dbClient.find(JsonObject.class, email);
		return json;	
	}

}
